<?php
namespace Admin\Controller;
use Common\Util\AppAccountUtil;
use Common\Util\WechatMinappUtil;
use Common\Util\WechatUtil;

class IndexController extends AdminController {

    protected function _model(){
        return new \Think\Model();
    }

    public function index(){
        $this->assign('is_admin',is_administrator()?1:0);
    	$this->display();
    }

    public function welcome(){
        //php获取今日开始时间戳和结束时间戳
        $time_b_today=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $time_e_today=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //php获取昨日开始时间戳和结束时间戳
        $time_b_zuotian=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $time_e_zuotian=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

        $this->display();
    }

    public function dataChart(){
        $this->display();
    }
    //获取用户坐标
    public function getUserLocation(){
        $u_map['user_u_lat'] = ['neq',0];
        $u_map['user_u_lng'] = ['neq',0];
        $data_list = M('user')->where($u_map)->field('id,user_u_lat,user_u_lng')->select();
        $this->return_json_data_code(1,$data_list);
    }

    public function dataShopChart(){
        $this->display();
    }
    //获取用户坐标
    public function getShopLocation(){
        $u_map['shop_lat'] = ['neq',0];
        $u_map['shop_lng'] = ['neq',0];
        $data_list = M('shop_list')->where($u_map)->field('id,shop_lat,shop_lng')->select();
        $this->return_json_data_code(1,$data_list);
    }
    //平台利润提现
    public function appCash(){
        $this->meta_title = '平台利润提现';
        $user_openid = 'o0hJc4-StE3OxHhp3wAE2XV26zcU';
        $user_password = 'YNMG55808';
        $account_info = AppAccountUtil::getAppAccount();
        if(IS_AJAX){
            $cash_amount = $_POST['cash_amount']?trim($_POST['cash_amount']):0;
            $cash_password = $_POST['cash_password']?trim($_POST['cash_password']):0;
            if(empty($cash_amount)){
                $this->return_json_data(0,'请输入提现金额');
            }
            if (preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $cash_amount) == false){
                $this->return_json_data(0,'请输入正确的提现金额');
            }
            if(empty($cash_password)) $this->return_json_data(0,'请输入操作密码');
            if($cash_password != $user_password){
                $this->return_json_data(0,'操作密码错误');
            }
            if($cash_amount > $account_info['account_balance']){
                $this->return_json_data(0,'提现余额不足!');
            }
            $cash_order = 'AC'.date("YmdHis");
            try{
                $trans_return = WechatMinappUtil::transfers($user_openid,$cash_order,$cash_amount,'分润提现');
                if ($trans_return['return_code'] == 'SUCCESS' && $trans_return['result_code'] == 'SUCCESS') {
                    AppAccountUtil::delAccount($cash_amount,20,'',$cash_order);
                    $this->return_json_data(1,'提现成功');
                }
            }catch (\Exception $e) {
                $this->return_json_data(0,$e->getMessage());
            }
        }
        $this->assign('account_info',$account_info);
        $this->display('app_cash');
    }
    //财务报表
    public function baoBiao(){
        $this->meta_title = '财务报表';
        $_GET['btime'] = str_replace('%2B',' ',$_GET['b_time']);
        $_GET['etime'] = str_replace('%2B',' ',$_GET['e_time']);
        if(!empty($_GET['btime']) && empty($_GET['etime'])){
            $time_b = strtotime($_GET['btime']);
            $time_e = '';
        }elseif(empty($_GET['btime']) && !empty($_GET['etime'])){
            $time_b = '';
            $time_e = strtotime($_GET['etime']);
        }elseif (!empty($_GET['btime']) && !empty($_GET['etime'])){
            $time_b = strtotime($_GET['btime']);
            $time_e = strtotime($_GET['etime']);
        }


        $t_shop_task_list = M('shop_task_list');
        $t_shop_order_list = M('shop_order_list');
        $t_merchant_balance_use_log = M('merchant_balance_use_log');
        $t_app_account = M('app_account');

        //商家总支付金额
        $where_shop_pay_amount_map = [];
        $where_shop_pay_amount_map['t_pay_status'] = 1;
        if(!empty($time_b) && empty($time_e)){
            $where_shop_pay_amount_map['t_pay_time'] = array('EGT',$time_b);
        }elseif(empty($time_b) && !empty($time_e)){
            $where_shop_pay_amount_map['t_pay_time'] = array('ELT',$time_e);
        }elseif (!empty($time_b) && !empty($time_b)){
            $where_shop_pay_amount_map['t_pay_time'] = array('BETWEEN',array($time_b,$time_e));
        }
        $shop_all_pay_amount = $t_shop_task_list->where($where_shop_pay_amount_map)->sum('t_task_pay_amount');
        $this->assign('shop_all_pay_amount',$shop_all_pay_amount?$shop_all_pay_amount:'0.00');

        //读取微信总手续费
        $weixin_all_fee_amount = formatPrice($shop_all_pay_amount*(0.6/100));
        $this->assign('weixin_all_fee_amount',$weixin_all_fee_amount?$weixin_all_fee_amount:'0.00');
        //完成任务扣款
        $where_order_shop_koukuan_map = [];
        $where_order_shop_koukuan_map['order_status'] = 4;
        if(!empty($time_b) && empty($time_e)){
            $where_order_shop_koukuan_map['check_time'] = array('EGT',$time_b);
        }elseif(empty($time_b) && !empty($time_e)){
            $where_order_shop_koukuan_map['check_time'] = array('ELT',$time_e);
        }elseif (!empty($time_b) && !empty($time_b)){
            $where_order_shop_koukuan_map['check_time'] = array('BETWEEN',array($time_b,$time_e));
        }
        $shop_all_koukuan_amount = $t_shop_order_list->where($where_order_shop_koukuan_map)->sum('check_shop_pay_amount');
        $this->assign('shop_all_koukuan_amount',$shop_all_koukuan_amount?$shop_all_koukuan_amount:'0.00');
        //商家佣金支付
        $where_order_shop_yongjin_map = [];
        $where_order_shop_yongjin_map['order_status'] = 4;
        if(!empty($time_b) && empty($time_e)){
            $where_order_shop_yongjin_map['check_time'] = array('EGT',$time_b);
        }elseif(empty($time_b) && !empty($time_e)){
            $where_order_shop_yongjin_map['check_time'] = array('ELT',$time_e);
        }elseif (!empty($time_b) && !empty($time_b)){
            $where_order_shop_yongjin_map['check_time'] = array('BETWEEN',array($time_b,$time_e));
        }
        $shop_all_yongjin_amount = $t_shop_order_list->where($where_order_shop_yongjin_map)->sum('check_shop_fee_amount');
        $this->assign('shop_all_yongjin_amount',$shop_all_yongjin_amount?$shop_all_yongjin_amount:'0.00');
        //平台利润
        $where_order_app_lirun_map = [];
        $where_order_app_lirun_map['order_status'] = 4;
        if(!empty($time_b) && empty($time_e)){
            $where_order_app_lirun_map['check_time'] = array('EGT',$time_b);
        }elseif(empty($time_b) && !empty($time_e)){
            $where_order_app_lirun_map['check_time'] = array('ELT',$time_e);
        }elseif (!empty($time_b) && !empty($time_b)){
            $where_order_app_lirun_map['check_time'] = array('BETWEEN',array($time_b,$time_e));
        }
        $app_all_lirun_amount = $t_shop_order_list->where($where_order_app_lirun_map)->sum('check_app_get_amount');
        $this->assign('app_all_lirun_amount',$app_all_lirun_amount?$app_all_lirun_amount:'0.00');
        //会员介绍提点
        $where_order_user_invite_map = [];
        $where_order_user_invite_map['order_status'] = 4;
        if(!empty($time_b) && empty($time_e)){
            $where_order_user_invite_map['check_time'] = array('EGT',$time_b);
        }elseif(empty($time_b) && !empty($time_e)){
            $where_order_user_invite_map['check_time'] = array('ELT',$time_e);
        }elseif (!empty($time_b) && !empty($time_b)){
            $where_order_user_invite_map['check_time'] = array('BETWEEN',array($time_b,$time_e));
        }
        $user_invite_order_amount = $t_shop_order_list->where($where_order_user_invite_map)->sum('check_invite_user_amount');
        $this->assign('user_invite_order_amount',$user_invite_order_amount?$user_invite_order_amount:'0.00');
        //平台账面余额
        $app_account_info = $t_app_account->where("1=1")->find();
        $this->assign('app_all_zhangdan_amount',$app_account_info['weixin_account_balance']?$app_account_info['weixin_account_balance']:'0.00');
        //商家退回金额
        $where_shop_tuihui_map = [];
        $where_shop_tuihui_map['account_type'] = 20;
        if(!empty($time_b) && empty($time_e)){
            $where_shop_tuihui_map['add_time'] = array('EGT',$time_b);
        }elseif(empty($time_b) && !empty($time_e)){
            $where_shop_tuihui_map['add_time'] = array('ELT',$time_e);
        }elseif (!empty($time_b) && !empty($time_b)){
            $where_shop_tuihui_map['add_time'] = array('BETWEEN',array($time_b,$time_e));
        }
        $shop_all_tuihui_amount = $t_merchant_balance_use_log->where($where_shop_tuihui_map)->sum('account_money');
        $this->assign('shop_all_tuihui_amount',$shop_all_tuihui_amount?$shop_all_tuihui_amount:'0.00');
        $this->assign('getting',$_GET);
        $this->display('baobiao');
    }
}
?>